package com.android.djlmapp.adapter.kaika;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.djlmapp.R;
import com.android.djlmapp.holder.JiTuanHolder;
import com.android.djlmapp.model.kaika.JiTuanOrGuHuaInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 宽带套餐数据适配器
 * 2017/12/12.
 */

public class JiTuanBianMaAdapter extends RecyclerView.Adapter<JiTuanHolder> {

    private List<JiTuanOrGuHuaInfo.ListBean> list = new ArrayList<>();
    private LayoutInflater mInflater;
    private Context mContext;
    private Activity activity;
    private boolean isJiTuan;
    private String backValue;
    private String backAddParaValue;


    public JiTuanBianMaAdapter(Context context, boolean isJiTuan) {
        this.mContext = context;
        this.activity = (Activity) context;
        this.isJiTuan = isJiTuan;

        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<JiTuanOrGuHuaInfo.ListBean> list) {
        this.list = list;
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        notifyDataSetChanged();
    }


    public OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickListener = mOnItemClickLitener;
    }


    public BackClickValueListener mBackClickValueListener;

    public interface BackClickValueListener {
        void backClickValue(String value, String addPara);
    }

    public void setBackClickValueListener(BackClickValueListener mBackClickValueListener) {
        this.mBackClickValueListener = mBackClickValueListener;
    }

    @Override
    public JiTuanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_jituan_popupwindow, parent, false);

        JiTuanHolder holder = new JiTuanHolder(view);
        holder.rl_jtbm = view.findViewById(R.id.rl_jtbm);
        holder.jt_tv_num = view.findViewById(R.id.jt_tv_num);
        holder.jt_tv_address = view.findViewById(R.id.jt_tv_address);
        holder.jt_iv_jt = view.findViewById(R.id.jt_iv_jt);

        if (isJiTuan) { // 如果是集团编码就显示地址栏,是固话号码就隐藏
            holder.jt_tv_address.setVisibility(View.VISIBLE);
        } else {
            holder.jt_tv_address.setVisibility(View.GONE);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final JiTuanHolder holder, final int position) {

        if (isJiTuan) {
            holder.jt_tv_num.setText(list.get(position).getJituan_no());
            holder.jt_tv_address.setText(list.get(position).getTitle());
        } else {
            holder.jt_tv_num.setText(list.get(position).getMobileNumber());
        }

        holder.rl_jtbm.setTag(position);
        holder.rl_jtbm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isJiTuan) {
                    backValue = list.get(position).getJituan_no();
                    backAddParaValue = "";
                } else {
                    backValue = list.get(position).getMobileNumber();
                    backAddParaValue = list.get(position).getAdd_para();
                }
                mBackClickValueListener.backClickValue(backValue, backAddParaValue);// 返回编码

            }
        });

    }

    @Override
    public int getItemCount() {
        if (list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }
}
